const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    loupan: [],
    servicePhone: '',
    couponShow: false,
    is_show_coupon: false,
    customerService: false,
    orders: ['demo-text-1', 'demo-text-2', 'demo-text-3']
  },

  callPhone(e) {
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync('phone'),
    })
  },
  /**
   * 抢红包
   */
  couponTap() {
    this.setData({
      couponShow: true
    })
  },
  customerServiceHide() {
    this.setData({
      customerService: false
    })
  },
  customerServiceShow() {
    this.setData({
      customerService: true
    })
  },

  customerServiceStop(e) {
    // e.preventDefault();
  },
  /**
   * 打开新人礼包
   */
  onNewGift(obj) {
  
    var that = this,
      type = obj.currentTarget.dataset.type,
      user_id = wx.getStorageSync('user_id'),
      login_status = wx.getStorageSync('login_status');
    // if(type == false){
    //   wx.showToast({
    //     title: '暂时没有可领取优惠券',
    //     icon: 'none',
    //     time: 1500
    //   })
    //   setTimeout(() => {
    //     that.closeExclusive();
    //   }, 1500);
    //   return false;
    // }
    if (login_status && login_status > 0) {
      https.post('getCoupon', {
        user_id: user_id,
        coupon_type: 'new'
      }, false, '正在领取').then(res => {
        if(res){
          that.setData({
            couponShow: false,
            is_show_coupon: false
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/grant/grant',
      })
    }
  },
  /**
   * 关闭新人礼包
   */
  closeExclusive() {
    this.setData({
      couponShow: false,
      is_show_coupon: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.scene) {
      app.globalData.promote_user_id = options.scene; // 推荐人
    }
    if (options.goods_id) {
     
      wx.redirectTo({
        url: '/pages/goods_info/goods_info?goods_id='+ options.goods_id,
        
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
      login_status = wx.getStorageSync('login_status'), //登陆状态
      user_id = wx.getStorageSync('user_id'); //用户ID
    if (login_status && login_status > 0) {
      //开始查看 是否存在未领取的新用户专享优惠券
      // https.post('checkNewUserCoupon', {
      //   user_id: user_id
      // }, true).then(res => {
      //   if (!res) {
      //     that.setData({
      //       couponShow: false,
      //       is_show_coupon: false
      //     })
      //   } else {
      //     that.setData({
      //       couponShow: true,
      //       is_show_coupon: true
      //     })
      //   }
      // })
    }
    that.getBanner();
    that.getNav();
    that.getHome();
    that.setLogin();
    that.getLoupan()
  },
  /**
   * @xk 获取banner图片
   */
  getBanner: function () {
    var that = this;
    https.post('getBanner', {
      pagesize: 4
    }).then(res => {
      this.setData({
        banner: res
      })
    })
  },
  /**
   * @xk 获取导航
   */
  getNav: function () {
    var that = this;
    https.post('getNav', {}, false).then(res => {
      this.setData({
        nav: res
      })
    })
  },
  /**
   * @xk 加载首页内容
   */
  getHome: function () {
    var that = this;
    https.post('getHomeInfo', {}, false).then(res => {
      this.setData({
        homeInfo: res
      })
    })
  },
  getLoupan: function () {
    var that = this;
    https.post('getLoupan', {}, false).then(res => {
      this.setData({
        loupan: res
      })
    })
  },
  /**
   *
   * @xk 执行跳转
   */
  navJump: function (obj) {


  var   goods_id = obj.currentTarget.dataset.goodsid;
     // url = obj.currentTarget.dataset.url;
  var   url =  '/pages/goods_info/goods_info?goods_id='+goods_id;
     console.info(obj.currentTarget.dataset)
    if (goods_id) {
     
          wx.navigateTo({
            url: url,
          })
          
        
      
    }
  },
  setLogin: function (res) {
    var that = this,
      user_id = wx.getStorageSync('user_id');
    if (user_id > 0) {
      https.post('setLogins', {
        user_id: user_id
      }, true).then(res => {});
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var timestamp = Date.parse(new Date()),
      app_name = wx.getStorageSync('app_name'),
      app_logo = wx.getStorageSync('app_logo'),
      user_id = wx.getStorageSync('user_id'),
      url = '/pages/home/home',
      timestr = timestamp / 1000;
    url += '?timestr=' + timestr;
    if (user_id > 0) {
      url += '&l_id=' + user_id;
    }
    return {
      title: app_name,
      path: url,
      imageUrl: app_logo
    }
  }
})
